package com.hxrfid.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.utils.StringUtils;
import com.hxrfid.project.domain.BsOrderProduct;
import com.hxrfid.project.domain.bo.BsOrderProductBo;
import com.hxrfid.project.domain.vo.BsOrderProductVo;
import com.hxrfid.project.mapper.BsOrderProductMapper;
import com.hxrfid.project.service.IBsOrderProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 订单商品Service业务层处理
 *
 * @author hxrfid
 * @date 2025-06-30
 */
@RequiredArgsConstructor
@Service
public class BsOrderProductServiceImpl extends ServiceImpl<BsOrderProductMapper, BsOrderProduct> implements IBsOrderProductService {

    private final BsOrderProductMapper baseMapper;

    /**
     * 查询订单商品
     * @param id 主键ID
     * @return 订单商品
     */
    @Override
    public BsOrderProductVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单商品列表
     * @param bo 订单商品对象
     * @param pageQuery 主键ID
     * @return 订单商品列表
     */
    @Override
    public TableDataInfo<BsOrderProductVo> queryPageList(BsOrderProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsOrderProduct> lqw = buildQueryWrapper(bo);
        Page<BsOrderProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单商品列表
     * @param bo 订单商品对象
     * @return 订单商品列表
     */
    @Override
    public List<BsOrderProductVo> queryList(BsOrderProductBo bo) {
        LambdaQueryWrapper<BsOrderProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
    * 组装查询条件
    * @param bo 订单商品对象
    * @return 查询条件
    */
    private LambdaQueryWrapper<BsOrderProduct> buildQueryWrapper(BsOrderProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsOrderProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, BsOrderProduct::getOrderId, bo.getOrderId());
        lqw.eq(bo.getProductId() != null, BsOrderProduct::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getProductTitle()), BsOrderProduct::getProductTitle, bo.getProductTitle());
        lqw.eq(bo.getProductPrice() != null, BsOrderProduct::getProductPrice, bo.getProductPrice());
        lqw.eq(bo.getProductNum() != null, BsOrderProduct::getProductNum, bo.getProductNum());
        lqw.eq(bo.getTotalPrice() != null, BsOrderProduct::getTotalPrice, bo.getTotalPrice());
        lqw.eq(bo.getTenantId() != null, BsOrderProduct::getTenantId, bo.getTenantId());
        return lqw;
    }

    /**
     * 新增订单商品
     * @param bo 订单商品对象
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean insertByBo(BsOrderProductBo bo) {
        BsOrderProduct add = BeanUtil.toBean(bo, BsOrderProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单商品
     * @param bo 订单商品对象
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateByBo(BsOrderProductBo bo) {
        BsOrderProduct update = BeanUtil.toBean(bo, BsOrderProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     * @param entity 订单商品对象
     */
    private void validEntityBeforeSave(BsOrderProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单商品
     * @param ids 主键ID的list
     * @param isValid 是否业务校验
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
